Passed
Pull Request — master (#188)
by Mathieu
01:57
created

GetProjectsQueryHandler.execute   A

Complexity

Conditions 2

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 22
dl 0
loc 27
rs 9.352
c 0
b 0
f 0
1
import {QueryHandler} from '@nestjs/cqrs';
2
import {Inject} from '@nestjs/common';
3
import {GetProjectsQuery} from './GetProjectsQuery';
4
import {ProjectView} from '../View/ProjectView';
5
import {IProjectRepository} from 'src/Domain/Project/Repository/IProjectRepository';
6
import {CustomerView} from 'src/Application/Customer/View/CustomerView';
7
import {Pagination} from 'src/Application/Common/Pagination';
8
9
@QueryHandler(GetProjectsQuery)
10
export class GetProjectsQueryHandler {
11
  constructor(
12
    @Inject('IProjectRepository')
13
    private readonly projectRepository: IProjectRepository
14
  ) {}
15
16
  public async execute(
17
    query: GetProjectsQuery
18
  ): Promise<Pagination<ProjectView>> {
19
    const {customerId, page} = query;
20
21
    const projectViews: ProjectView[] = [];
22
    const [projects, total] = await this.projectRepository.findProjects(
23
      page,
24
      customerId
25
    );
26
27
    for (const project of projects) {
28
      const customer = project.getCustomer();
29
30
      projectViews.push(
31
        new ProjectView(
32
          project.getId(),
33
          project.getName(),
34
          project.getDayDuration(),
35
          project.getInvoiceUnit(),
36
          new CustomerView(customer.getId(), customer.getName())
37
        )
38
      );
39
    }
40
41
    return new Pagination<ProjectView>(projectViews, total);
42
  }
43
}
44